import pytest
import requests

from api.reg_login_api import RegLoginApi
from api.tender_api import TenderApi
from common.assert_tools import common_assert
from common.db_tools import init_tender_info
from common.read_html_tools import html_util
from common.read_json_data import read_json


class TestTender(object):

    @staticmethod
    def setup_class():
        # 借款标测试数据初始化
        init_tender_info()

    def setup(self):
        # 创建session对象
        self.ses = requests.session()
        # 调用投资模块类
        self.tender = TenderApi(self.ses)
        # 调用登录模块类
        self.login = RegLoginApi(self.ses)

    def teardown(self):
        # 关闭session对象
        self.ses.close()

    # 投资测试用例
    @pytest.mark.parametrize("phone, password, id, amount, exp_status_code, exp_status, exp_description",
                             read_json("tender_data", "tender"))
    def test01_tender(self, phone, password, id, amount, exp_status_code, exp_status, exp_description):
        # 用户登录
        self.login.user_login(phone, password)
        # 投资
        resp = self.tender.tender(id, amount)
        common_assert(resp, exp_status_code, exp_status, exp_description)

    # 第三方投资测试用例
    def test02_third_tender(self):
        self.login.user_login("13155552220", "qq1234")
        resp_tender = self.tender.tender(642, 1000)
        result = html_util(resp_tender)
        url = result[0]
        form_dict = result[1]
        resp = self.tender.third_tender(url, form_dict)
        assert 200 == resp.status_code
        assert "OK" in resp.text
